const _ = require('lodash')
const moment = require('moment')

moment.locale('en', {
    relativeTime: {
        future: "%s前",
        past: "刚刚",
        s: "不久",
        m: "1分种",
        mm: "%d分种",
        h: "1小时",
        hh: "%d小时",
        d: "1天",
        dd: "%d天",
        M: "1月",
        MM: "%d月",
        y: "1年",
        yy: "%d年"
    }
})

moment.current = function () {
    return this().format("YYYY-MM-DD HH:mm:ss")
}

class Feedback {
    format() {

    }

    success(data) {
        return {
            code: 0,
            data: data,
            status: true
        }
    }

    error(message = '系统异常', code = 1000) {
        return {
            code: 1000,
            message: message,
            status: false
        }
    }

    errorHandle(err) {
        return err.original.sqlMessage
    }

    unAuth() {
        return {
            code: 1001,
            message: '用户认证失败'
        }
    }

    callback(prom, options = {}) {
        return prom
            .then(data => {
                if (options.then) {
                    return options.then.call(this, data)
                } else {
                    return this.success(data)
                }
            })
            .catch(err => {
                if (err.original) {
                    console.error(err.original.sqlMessage)
                    return this.error(err.original.sqlMessage || '系统异常')
                } else {
                    console.error(err)
                    if (options.catch) {
                        return options.catch()
                    } else {
                        return this.error(err.message)
                    }
                }
            })
    }
    
}

class Dev {
    clearNull (obj) {
        return _.omitBy(obj, e => {
            return !(e || e === 0)
        })
    }
}

module.exports = {
    Fb: new Feedback(),
    Op: require('sequelize').Op,
    Moment: moment,
    Dev: new Dev()
}